package com.shengwei.truck.driver.vm;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.shengwei.truck.driver.bean.JsonCar;
import com.shengwei.truck.driver.bean.Result;
import com.shengwei.truck.driver.model.DataSource;
import com.shengwei.truck.driver.model.Repository;

import java.util.ArrayList;

/**
 * 车辆信息管理的VM层，调用M层去获取数据，同时更改liveData的数据，从而View层就能观察到LiveData数据的
 * 变化，最后更新UI。
 */
public class CarInfoViewModel extends ViewModel {
    private final Repository mRepository;
    String TAG = "CarInfoViewModel";
    private MutableLiveData<Result<ArrayList<JsonCar>>> mSearchLiveData = new MutableLiveData<>();
    private MutableLiveData<Result<JsonCar>>mCarDetailLiveData = new MutableLiveData<>();
    private MutableLiveData<Result<String>>mBindCarLiveData = new MutableLiveData<>();
    private MutableLiveData<Result<JsonCar>>mCarInfoPageLiveData = new MutableLiveData<>();
    /**
     * 构造函数
     */
    public CarInfoViewModel() {
        mRepository = Repository.getInstance(DataSource.getSingleInstance());
    }

    public MutableLiveData<Result<ArrayList<JsonCar>>> getSearchLiveData() {
        return mSearchLiveData;
    }
    public MutableLiveData<Result<JsonCar>> getCarInfoPageLiveData() { return mCarInfoPageLiveData; }
    public MutableLiveData<Result<String>> getBindCarLiveData() {
        return mBindCarLiveData;
    }
    public MutableLiveData<Result<JsonCar>> getCarDetailLiveData() {
        return mCarDetailLiveData;
    }
    /**
     * 模糊查询车辆名称
     * @param keyWord：键入的关键字
     */
    public void searchCarName(String keyWord) {
        mRepository.searchCarName(keyWord,mSearchLiveData);
    }

    /**
     * 根据车辆id，获取车辆相关的厂商，品牌，载重等信息
     * @param carId
     */
    public void  getCarDetail(String carId) {
        mRepository.getCarDetail(carId,mCarDetailLiveData);
    }

    /**
     * 绑定车辆信息
     * @param carNum： 车辆号
     * @param carId： 车辆名称ID
     * @param isEtcNum: 是否有etc，1有，0无
     * @param durableYears: 使用年限
     */
    public void bindCarInfo(String carNum,String carId,int isEtcNum,String durableYears) {
        mRepository.bindCarInfo(carNum,carId,isEtcNum,durableYears,mBindCarLiveData);
    }

    /**
     * 获取 "车辆信息"页面的数据：包括车型自带的厂商品牌信息，还包括用户手动录入的信息如车牌号等
     */
    public void getMyCarInfo() {
        mRepository.getMyCarInfo(mCarInfoPageLiveData);
    }


}